# containers.py
from dependency_injector import containers, providers
from .services import UserRepository, EmailService, RegistrationService


class DatabaseUserRepository(UserRepository):
    def save(self, user):
        print("Saving user to database...")
        return f"Saved user {user['name']}"


class MockUserRepository(UserRepository):
    def save(self, user):
        print("Mock saving user (test mode)...")
        return f"Mock saved user {user['name']}"


class SmtpEmailService(EmailService):
    def send_email(self, email, message):
        print(f"[SMTP] Sending email to {email}: {message}")
        return True


class ConsoleEmailService(EmailService):
    def send_email(self, email, message):
        print(f"[Console] Simulating sending email to {email}: {message}")
        return True


class Container(containers.DeclarativeContainer):
    # 用户存储方式可配置
    user_repository = providers.Factory(
        DatabaseUserRepository
    )

    # 邮件服务可替换
    email_service = providers.Factory(
        ConsoleEmailService
    )

    # 注册服务自动注入依赖
    registration_service = providers.Factory(
        RegistrationService,
        user_repo=user_repository,
        email_service=email_service
    )